#include "msgbox.h"
#include "ui_msgbox.h"

MsgBox::MsgBox(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MsgBox)
{
    ui->setupUi(this);

    connect(ui->btnOk, &QAbstractButton::clicked, this, &MsgBox::clickSlot);
    connect(ui->btnCancel, &QAbstractButton::clicked, this, &MsgBox::clickSlot);
    connect(ui->btnClose, &QAbstractButton::clicked, this, &MsgBox::clickSlot);

    initWidget();
}

MsgBox::~MsgBox()
{
    delete ui;
}

void MsgBox::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.fillRect(this->rect(), QColor(255, 255, 255, 0));
}

QHBoxLayout *MsgBox::getButtonLayout()
{

    return static_cast<QHBoxLayout*>(ui->btnWidget->layout());
}

void MsgBox::setTitle(const QString &title)
{
    ui->title->setText(title);
}

void MsgBox::setBtnCancelVisible(bool state)
{
    ui->btnCancel->setVisible(state);
}

void MsgBox::setBtnOkVisible(bool state)
{
    ui->btnOk->setVisible(state);
}

void MsgBox::setMenuVisible(bool state)
{
    ui->menu->setVisible(state);
}

QString MsgBox::text()
{
    return ui->textLabel->text();
}

void MsgBox::setText(const QString &text)
{
    ui->textLabel->setText(text);
    //todo:Qt::mightBeRichText error?
//    ui->textLabel->setWordWrap(
//                ui->textLabel->textFormat()== Qt::RichText ||
//                (ui->textLabel->textFormat()== Qt::AutoText &&Qt::mightBeRichText(text) ));
}

void MsgBox::setTextStyleSheet(const QString &styleSheet)
{
    ui->textLabel->setStyleSheet(styleSheet);
}

QString MsgBox::informativeText()
{
    return ui->informativeLabel->text();
}

void MsgBox::setInformativeText(const QString &text)
{
    ui->informativeLabel->setText(text);
}

QString MsgBox::getBtnCancelStyleSheet()
{
    return ui->btnCancel->styleSheet();
}

QString MsgBox::getBtnOkStyleSheet()
{
    return ui->btnOk->styleSheet();
}

void MsgBox::hideInformation()
{
    if (windowWidth >= 1280) {
        ui->textLabel->setMinimumHeight(90);
        ui->textLabel->setMaximumHeight(90);
    } else {
        ui->textLabel->setMinimumHeight(70);
        ui->textLabel->setMaximumHeight(70);
    }

    ui->informBox->setVisible(false);
}

int MsgBox::inform(QWidget *parent, const QString &title, const QString &text)
{
    ui->title->setText(title);
    setText(text);
    ui->btnCancel->setVisible(false);
    ui->btnOk->setFocus();
    this->exec();
    return state;
}

int MsgBox::warn(QWidget *parent, const QString &title, const QString &text)
{
    ui->title->setText(title);
    setText(text);
    this->exec();
    return state;
}

QMessageBox::StandardButton MsgBox::information(QWidget *parent,
                                                const QString &title,
                                                const QString &text,
                                                QMessageBox::StandardButtons buttons,
                                                QMessageBox::StandardButton defaultButton)
{
    MsgBox msgbox;
    int state = msgbox.inform(parent,title,text);
    return static_cast<QMessageBox::StandardButton>(state);
}

QMessageBox::StandardButton MsgBox::warning(QWidget *parent,
                                            const QString &title,
                                            const QString &text,
                                            QMessageBox::StandardButtons buttons,
                                            QMessageBox::StandardButton defaultButton)
{
    MsgBox msgbox;
    int state = msgbox.warn(parent,title,text);
    return static_cast<QMessageBox::StandardButton>(state);
}

void MsgBox::clickSlot()
{
    QAbstractButton* btn = qobject_cast<QAbstractButton*>(sender());
    if (btn->objectName() == "btnOk"){
        state = QMessageBox::Ok;
    }
    else if(btn->objectName() == "btnCancel"){
        state = QMessageBox::Cancel;
    }
    close();
}

int MsgBox::exec()
{
    QDialog::exec();
    return state;
}

void MsgBox::initWidget()
{
    setAttribute(Qt::WA_TranslucentBackground, true);
    if (windowWidth >= 1280) {
        this->setProperty("cssSize", "large");
        this->setGeometry(0, 0, 1280, 800);
        ui->btnWidget->setMinimumHeight(80);
        ui->menu->setMinimumHeight(66);
    } else {
        this->setGeometry(0, 0, 1024, 600);
    }
    this->setWindowFlags(this->windowFlags() | Qt::SubWindow);
    this->setParent(Project::Instance());
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
    shadow->setColor(QColor(0,0,0,150));
    shadow->setOffset(1, 1);
    shadow->setBlurRadius(16);
    ui->widget->setGraphicsEffect(shadow);
//    this->setProperty("form", true);
//    this->setProperty("canMove", true);
//    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
}
